﻿package com.lemon.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lemon.db.BlackTable;
import com.lemon.db.Config;
import com.lemon.db.DB;
import com.lemon.db.UserInfo;
import com.lemon.tools.Constants;

/**
 * @author lemonjesnme
 * @category init tables
 */
public class C2S0init extends HttpServlet {
	@Override
	public void init() throws ServletException {
		Constants.init();
		System.out.println("DB name:" + Constants.DBNAME);
		DB.connDB();
		super.init();
	}

	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		Enumeration<String> er = request.getParameterNames();
		System.out.println("[vswb] accept the data begin");
		long user_id = 1;
		String userPhoneNumber = null;
		while (er.hasMoreElements()) {

		}
		PrintWriter writer = response.getWriter();
		writer.println("<p>初始化后台服务 ok</p>");
		writer.println("<p>初始化后台数据库 ok</p>");
		ResultSet rs = null;
		try{
			rs = DB.select("select cleanDB from config");
		}catch(Exception e){
			
		}
		String cleanDB = null;
		try {
			while (rs.next()) {
				cleanDB = rs.getString("cleanDB");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if ((cleanDB != null) && (cleanDB.equals("true"))) {
			DB.delete("drop table UserInfo;");
			DB.delete("drop table BlackTable;");
			DB.delete("drop table Config;");
			DB.createTable(UserInfo.class);
			DB.createTable(BlackTable.class);
			DB.createTable(Config.class);
			int userTotalID = 0;
			DB.insert("insert into config(cleanDB,userTotalID) values('false','"+String.valueOf(userTotalID)+"');");
		}else if((cleanDB != null) && (cleanDB.equals("false"))){
			ResultSet rs1 = DB.select("select userTotalID from config;");
			String userTotalID1 = null;
			try {
				while(rs1.next()){
					userTotalID1 = rs1.getString("userTotalID");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			int id = Integer.valueOf(userTotalID1);
			id++;
			DB.update("update config set userTotalID='"+String.valueOf(id)+"';");
		}
	}
}
